package com.ovo.chatRobot.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ovo.chatRobot.service.BaseTalkService;
import com.ovo.chatRobot.utils.RedisUtil;
import com.ovo.chatRobot.utils.VoiceToText;

@RestController
public class TalkController {
	@Autowired
	private BaseTalkService talkService;
	@Autowired
	private VoiceToText voiceToText;
	@Autowired
    private RedisTemplate redisTemplate;
	

	@RequestMapping(value = "/toTalk", method = RequestMethod.POST)
	public ModelMap toTalk(HttpServletRequest request, HttpServletResponse response) {
		ModelMap result = new ModelMap();
		String words = request.getParameter("words");
		String robotWords = talkService.toTalkAbout(words);
		result.put("msg", robotWords);
		return result;
	}

	@RequestMapping(value = "/toSendVoice", method = RequestMethod.POST)
	public ModelMap toSendVoice(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {
		ModelMap result = new ModelMap();
		voiceToText.init();
		Thread.sleep(2000);
		boolean returnFlag = true;
		String req_words="";
		String rep_words="";	
		while(returnFlag) {
			req_words=(String) redisTemplate.opsForValue().get("req_words");
			rep_words=(String) redisTemplate.opsForValue().get("rep_words");
			returnFlag = (null==req_words||"".equals(req_words))||(null==rep_words||"".equals(rep_words));				
		}
		System.out.println("***对话回成功了****："+req_words+"/"+rep_words);
		redisTemplate.delete("req_words");
		redisTemplate.delete("rep_words");
		result.put("reqWords", req_words);
		result.put("repWords", rep_words);
		return result;
	}

}
